// 导入http模块
const http = require('http');
// 导入mysql模块
const mysql = require('mysql');
// 设置域名
const hostname = '192.168.0.110';
// 设置端口号
const port = 3000;

// 创建服务器
const server = http.createServer((req, res) => {
    // 设置状态码
    res.statusCode = 200;
    // 设置请求头
    res.setHeader('Content-Type', 'text/plain; charset=utf-8');
    // 解决跨域问题
    res.setHeader("Access-Control-Allow-Origin", "*"); 
    // 连接数据库
    const connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: '123456',
        database: 'customize'
    });
    connection.connect();
    // 查询表
    connection.query('SELECT * FROM user', (err, rows) => {
        if (err) throw err;
        let data = JSON.stringify(rows)
        res.end(data);
    });
    connection.end();
});

// 建立连接
server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});